package com.briup.estore.web.controller;

import com.briup.estore.service.BookService;
import com.briup.estore.vo.BookInfoVO;
import com.briup.estore.vo.BookVO;
import com.briup.estore.web.response.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@Api(tags = "图书模块")
@RequestMapping("/book")
public class BookController {
    @Resource
    private BookService bookService;

    @GetMapping("showBooks")
    @ApiOperation("首页展示图书信息 只展示20本")
    public Result showBooks() {
        List<BookVO> books = bookService.showBooks();
        return Result.success(books);
    }

    @GetMapping("searchBook")
    @ApiOperation("图书信息检索根据书名、作者、出版社")
    public Result searchBook(@RequestParam("query") String query) {
        List<BookVO> book = bookService.searchBooks(query);
        return Result.success(book);
    }

    @GetMapping("findByCategory/{id}")
    @ApiOperation("图书信息检索根据分类")
    public Result findByCategory(@PathVariable Integer id) {
        List<BookVO> book = bookService.findByCategory(id);
        return Result.success(book);
    }

    @GetMapping("{id}")
    @ApiOperation("图书详情获取")
    public Result selectbyID(@PathVariable int id, HttpServletRequest request) {
        String userId = (String) request.getAttribute("userId");
        BookInfoVO book = bookService.selectbyID(userId,id);
        return Result.success(book);
    }

}